﻿Ext.define("PSI.InPlan.TraceForm", {
    createUI: function (entity) {
        var me = this;
        me.entity = entity;

        var wnd = Ext.create("Ext.window.Window", {
            title: "运输动态",
            modal: true,
            onEsc: Ext.emptyFn,
            width: 470,
            height: 570,
            layout: "fit",
            items: [
                {
                    id: "editForm",
                    xtype: "form",
                    layout: "form",
                    height: "100%",
                    bodyPadding: 5,
                    defaultType: 'textfield',
                    fieldDefaults: {
                        labelWidth: 100,
                        labelAlign: "right",
                        labelSeparator: "",
                        msgTarget: 'side'
                    },
                    items: [
                        {
                            xtype: "hidden",
                            name: "id",
                            value: entity.id
                        },
                        {
                            xtype: "displayfield",
                            value: entity.planNumber,
                            fieldLabel: "计划号"
                        },
                        {
                            xtype: "displayfield",
                            value: entity.address,
                            fieldLabel: "门点"
                        },
                        {
                            xtype: "displayfield",
                            value: entity.cntNumber,
                            fieldLabel: "箱号/封号"
                        },
                        {
                            xtype: "displayfield",
                            value: entity.cntType,
                            fieldLabel: "箱型"
                        },
                        {
                            xtype: "displayfield",
                            value: entity.deliverDT,
                            fieldLabel: "送货时限"
                        },
                        {
                            id: "editTruckCompany",
                            fieldLabel: "拖车公司",
                            xtype: "displayfield",
                            value: entity.truckCompany
                        },
                        {
                            id: "editTruckNumber",
                            xtype: "displayfield",
                            fieldLabel: "车号",
                            value: entity.truckNumber
                        },
                        {
                            id: "editDriver",
                            xtype: "displayfield",
                            name: "driver",
                            fieldLabel: "司机",
                            value: entity.driver
                        }, {
                            id: "editDriverTel",
                            xtype: "displayfield",
                            name: "driverTel",
                            fieldLabel: "司机电话",
                            value: entity.driverTel
                        }, {
                            xtype: "displayfield",
                            fieldLabel: "计划提箱时间",
                            value: entity.dt1Plan
                        },
                        {
                            xtype: "fieldcontainer",
                            fieldLabel: "实际提箱时间",
                            layout: "hbox",
                            items: [
                                {
                                    id: "editDT1Date",
                                    xtype: "datefield",
                                    format: "Y-m-d",
                                    name: "dt1Date"
                                }, {
                                    id: "editDT1Time",
                                    xtype: "timefield",
                                    format: "H:i",
                                    name: "dt1Time"
                                } , {
                                    id: "editDT1btn",
                                    xtype: "button",
                                    text: '当前时间'
                                }
                            ]
                        }, {
                            xtype: "displayfield",
                            fieldLabel: "计划出港时间",
                            value: entity.dt2Plan
                        },
                        {
                            xtype: "fieldcontainer",
                            fieldLabel: "实际出港时间",
                            layout: "hbox",
                            items: [
                                {
                                    id: "editDT2Date",
                                    xtype: "datefield",
                                    format: "Y-m-d",
                                    name: "dt2Date"
                                }, {
                                    id: "editDT2Time",
                                    xtype: "timefield",
                                    format: "H:i",
                                    name: "dt2Time"
                                }, {
                                    id: "editDT2btn",
                                    xtype: "button",
                                    text: '当前时间'
                                }
                            ]
                        }, {
                            xtype: "displayfield",
                            fieldLabel: "计划到卸货地时间",
                            value: entity.dt3Plan
                        }, {
                            xtype: "fieldcontainer",
                            fieldLabel: "实际到卸货地时间",
                            layout: "hbox",
                            items: [
                                {
                                    id: "editDT3Date",
                                    xtype: "datefield",
                                    format: "Y-m-d",
                                    name: "dt3Date"
                                }, {
                                    id: "editDT3Time",
                                    xtype: "timefield",
                                    format: "H:i",
                                    name: "dt3Time"
                                }, {
                                    id: "editDT3btn",
                                    xtype: "button",
                                    text: '当前时间'
                                }
                            ]
                        }, {
                            xtype: "displayfield",
                            fieldLabel: "计划卸货开始时间",
                            value: entity.dt5Plan
                        }, {
                            xtype: "fieldcontainer",
                            fieldLabel: "实际卸货开始时间",
                            layout: "hbox",
                            items: [
                                {
                                    id: "editDT5Date",
                                    xtype: "datefield",
                                    format: "Y-m-d",
                                    name: "dt5Date"
                                }, {
                                    id: "editDT5Time",
                                    xtype: "timefield",
                                    format: "H:i",
                                    name: "dt5Time"
                                }, {
                                    id: "editDT5btn",
                                    xtype: "button",
                                    text: '当前时间'
                                }
                            ]
                        }, {
                            xtype: "displayfield",
                            fieldLabel: "计划卸货完成时间",
                            value: entity.dt4Plan
                        }, {
                            xtype: "fieldcontainer",
                            fieldLabel: "实际卸货完成时间",
                            layout: "hbox",
                            items: [
                                {
                                    id: "editDT4Date",
                                    xtype: "datefield",
                                    format: "Y-m-d",
                                    name: "dt4Date"
                                }, {
                                    id: "editDT4Time",
                                    xtype: "timefield",
                                    format: "H:i",
                                    name: "dt4Time"
                                }, {
                                    id: "editDT4btn",
                                    xtype: "button",
                                    text: '当前时间'
                                }
                            ]
                        }, {
                            id: "editTraceMemo",
                            name: "traceMemo",
                            xtype: "textfield",
                            fieldLabel: "备注",
                            value: entity.traceMemo
                        }
                    ],
                    buttons: [
                        {
                            text: "确定",
                            formBind: true,
                            iconCls: "psi-button-ok",
                            handler: me.onOK,
                            scope: me
                        },
                        {
                            text: "取消", handler: function () {
                                PSI.MsgBox.confirm("请确认是否取消操作?", function () {
                                    me.wnd.close();
                                });
                            }, scope: me
                        }
                    ]
                }
            ]

        });

        me.wnd = wnd;

        var dat = new Date();
        var cc = Ext.Date.format(dat, 'Y-m-d');
        var ss = Ext.Date.format(dat, 'H:i');
        Ext.getCmp("editDT1btn").on("click", function () {
       
            Ext.getCmp("editDT1Date").setValue(cc);
            Ext.getCmp("editDT1Time").setValue(ss);
        }, me);
        Ext.getCmp("editDT2btn").on("click", function () {

            Ext.getCmp("editDT2Date").setValue(cc);
            Ext.getCmp("editDT2Time").setValue(ss);
        }, me);
        Ext.getCmp("editDT3btn").on("click", function () {

            Ext.getCmp("editDT3Date").setValue(cc);
            Ext.getCmp("editDT3Time").setValue(ss);
        }, me);
        Ext.getCmp("editDT4btn").on("click", function () {

            Ext.getCmp("editDT4Date").setValue(cc);
            Ext.getCmp("editDT4Time").setValue(ss);
        }, me);
        Ext.getCmp("editDT5btn").on("click", function () {

            Ext.getCmp("editDT5Date").setValue(cc);
            Ext.getCmp("editDT5Time").setValue(ss);
        }, me);
        Ext.getCmp("editDriver").on("specialkey", function (field, e) {
            if (e.getKey() == e.ENTER) {
                Ext.getCmp("editDriverTel").focus();
            }
        }, me);
        Ext.getCmp("editDriverTel").on("specialkey", function (field, e) {
            if (e.getKey() == e.ENTER) {
                Ext.getCmp("editDT1Date").focus();
            }
        }, me);
        Ext.getCmp("editDT1Date").on("specialkey", function (field, e) {
            if (e.getKey() == e.ENTER) {
                Ext.getCmp("editDT1Time").focus();
            }
        }, me);
        Ext.getCmp("editDT1Time").on("specialkey", function (field, e) {
            if (e.getKey() == e.ENTER) {
                Ext.getCmp("editDT2Date").focus();
            }
        }, me);
        Ext.getCmp("editDT2Date").on("specialkey", function (field, e) {
            if (e.getKey() == e.ENTER) {
                Ext.getCmp("editDT2Time").focus();
            }
        }, me);
        Ext.getCmp("editDT2Time").on("specialkey", function (field, e) {
            if (e.getKey() == e.ENTER) {
                Ext.getCmp("editDT3Date").focus();
            }
        }, me);
        Ext.getCmp("editDT3Date").on("specialkey", function (field, e) {
            if (e.getKey() == e.ENTER) {
                Ext.getCmp("editDT3Time").focus();
            }
        }, me);
        Ext.getCmp("editDT3Time").on("specialkey", function (field, e) {
            if (e.getKey() == e.ENTER) {
                Ext.getCmp("editDT4Date").focus();
            }
        }, me);
        Ext.getCmp("editDT4Date").on("specialkey", function (field, e) {
            if (e.getKey() == e.ENTER) {
                Ext.getCmp("editDT4Time").focus();
            }
        }, me);
        Ext.getCmp("editDT4Time").on("specialkey", function (field, e) {
            if (e.getKey() == e.ENTER) {
                Ext.getCmp("editTraceMemo").focus();
            }
        }, me);
    },

    show: function (parentForm, entity) {
        var me = this;

        me.parentForm = parentForm;

        me.createUI(entity);

        me.wnd.on("show", function () { me.onEditFormShow(entity); }, me);

        me.wnd.show();
    },

    onEditFormShow: function (entity) {
        Ext.getCmp("editDriver").focus();

        var me = this;
        var form = me.wnd;
        var el = me.wnd.getEl() || Ext.getBody();

        el.mask("数据加载中...");
        Ext.Ajax.request({
            url: "/InPlan/GetTraceInfo",
            method: "POST",
            params: { id: entity.id },
            callback: function (options, success, response) {
                el.unmask();
                if (success) {
                    var data = Ext.JSON.decode(response.responseText);
                    me.setEntityData(data);
                } else {
                    PSI.MsgBox.showInfo("网络错误");
                }
            }
        });
    },

    setEntityData: function (data) {
        Ext.getCmp("editDT1Date").setValue(data.dt1DriverDate);
        Ext.getCmp("editDT1Time").setValue(data.dt1DriverTime);
        Ext.getCmp("editDT2Date").setValue(data.dt2DriverDate);
        Ext.getCmp("editDT2Time").setValue(data.dt2DriverTime);
        Ext.getCmp("editDT3Date").setValue(data.dt3DriverDate);
        Ext.getCmp("editDT3Time").setValue(data.dt3DriverTime);
        Ext.getCmp("editDT4Date").setValue(data.dt4DriverDate);
        Ext.getCmp("editDT4Time").setValue(data.dt4DriverTime);
        Ext.getCmp("editDT5Date").setValue(data.dt5DriverDate);
        Ext.getCmp("editDT5Time").setValue(data.dt5DriverTime);

        Ext.getCmp("editTraceMemo").setValue(data.traceMemo);
    },

    // private
    onOK: function () {
        var me = this;

        var f = Ext.getCmp("editForm");
        var el = f.getEl() || Ext.getBody();
        el.mask("数据保存中...");
        f.submit({
            url: "/InPlan/Trace",
            method: "POST",
            success: function (form, action) {
                el.unmask();
                me.wnd.close();
                me.parentForm.refreshInPlanGrid();
            },
            failure: function (form, action) {
                el.unmask();
                PSI.MsgBox.showInfo(action.result.msg, function () {
                    Ext.getCmp("editDispatcher").focus();
                });
            }
        });
    }
});